package com.capgemini.diabeo.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.capgemini.diabeo.R;
import com.capgemini.diabeo.activity.HistoriqueMenuActivity_;
import com.capgemini.diabeo.activity.MenuActivity_;
import com.capgemini.diabeo.manager.DishManager;
import com.capgemini.diabeo.manager.DishManager_;
import com.capgemini.diabeo.manager.MealStatus;
import com.capgemini.diabeo.model.Dish;
import com.capgemini.diabeo.service.CategoryService_;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EViewGroup;
import com.googlecode.androidannotations.annotations.ViewById;

@EViewGroup(R.layout.dish_item)
public class DishItemView extends LinearLayout {

	@ViewById
	TextView dishName;

	@ViewById
	TextView dishCarbohydrate;

	@ViewById
	TextView dishIG;

	@ViewById
	TextView dishLipid;

	@ViewById
	Button addToMenu;

	@ViewById
	ImageView dishImage;

	@Bean
	DishManager dishManager;
	

	private Dish dish;

	public DishItemView(Context context) {
		super(context);
	}

	public void bind(Dish dish, boolean hasAddButton) {
		this.dish = dish;
		dishName.setText(dish.getName());
		dishCarbohydrate.setText(doubleToString(dish.getCarbohydrate()));
		dishIG.setText(doubleToString(dish.getIg()));
		dishLipid.setText(doubleToString(dish.getLipid()));
		if (dish.getImage()!=null)
		{
			Bitmap dishImageBitmap = retrieveImageFromBytes(dish.getImage());
			dishImage.setImageBitmap(dishImageBitmap);
		}
		
		if (hasAddButton) {
			addToMenu.setVisibility(VISIBLE);
		} else {
			addToMenu.setVisibility(GONE);
		}
	}

	private String doubleToString(double value) {
		return String.valueOf(value);
	}

	@Click
	public void addToMenuClicked(View view) {
		dishManager.addDishToMenu(dish);
		MenuActivity_.intent(getContext()).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
	}
	
	
	
	public static Bitmap retrieveImageFromBytes(byte[] image)
	{
		return BitmapFactory.decodeByteArray(image, 0, image.length);
	}

}
